import React, { Component } from 'react'

export default class Item extends Component {
  state = {
    mouse: false,
  }

  //鼠标移入移出
  handleMouse = (flag) => {
    return () => {
      this.setState({
        mouse: flag,
      })
    }
  }

  //修改状态
  handleChange = (id) => {
    return (e) => {
      this.props.updateTodo(id, e.target.checked)
    }
  }

  //删除
  handleClick = (id) => {
    return () => {
      this.props.removeTodo(id)
    }
  }

  render() {
    const { id, name, done } = this.props
    const { mouse } = this.state
    return (
      <li
        style={{ backgroundColor: mouse ? '#ddd' : '#fff' }}
        onMouseEnter={this.handleMouse(true)}
        onMouseLeave={this.handleMouse(false)}
      >
        <label>
          <input
            type="checkbox"
            checked={done}
            onChange={this.handleChange(id)}
          />
          <span>{name}</span>
        </label>
        <button
          className="btn btn-danger"
          style={{ display: mouse ? '' : 'none' }}
          onClick={this.handleClick(id)}
        >
          删除
        </button>
      </li>
    )
  }
}
